﻿using Ace;
using Chloe;
using Chloe.Infrastructure.Interception;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Chloe.Ext.Intf;
using Chloe.ObjectSpace;
using Chloe.Infrastructure;
using Chloe.Ext.stateMachine;
using System.Runtime.Loader;
using System.IO;

namespace Ace.Data
{
    public class OjbectSpaceFactory:DbContextFactory
    {

        private static int _cacheCount = 500;
        private static int _cacheExpireSeconds = 300;
        private static bool _useDbStateM = false;
        private static bool _useTrackEntity = true;
        private static Dictionary<string, wfStateMachine> smSchemeDic=null;
        private static IDbConnectionFactory dbconnFactory;
       
        static OjbectSpaceFactory()
        {
           

            _cacheCount = int.Parse(Globals.Configuration["db:CachCount"]);
            _cacheExpireSeconds = int.Parse(Globals.Configuration["db:CachExpireSeconds"]);
            var str = Globals.Configuration["db:useDbStateM"];
            if (string.IsNullOrEmpty(str))
                _useDbStateM = false;
            else
             _useDbStateM = bool.Parse(str);
             str = Globals.Configuration["db:useTrackEntity"];
            if (!string.IsNullOrEmpty(str))
                _useTrackEntity = bool.Parse(str);
            
            
        }

        public new static IContextSpace CreateContext()
        {
            IContextSpace dbContext = null;

            if (DbType == "sqlite")
            {
                dbContext = CreateSQLiteContext();
            }
            else if (DbType == "sqlserver")
            {
                dbContext = CreateSqlServerContext();
            }
            else if (DbType == "mysql")
            {
                dbContext = CreateMySqlContext();
            }
            else if (DbType == "oracle")
            {
                dbContext = CreateOracleContext();
            }
            else
            {
                dbContext = CreateSqlServerContext();
            }

            return dbContext;
        }

        public static void setSMSchemeDic(Dictionary<string, wfStateMachine> _smSchemeDic)
        {
            smSchemeDic = _smSchemeDic;
        }
        static  IContextSpace CreateSqlServerContext()
        {
            var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(SpaceFileName);
            var myType = myAssembly.GetType("Chloe.ObjectSpace.MssqlContextSpace");
            var cs = Activator.CreateInstance(myType, new object[] {ConnectionString ,_cacheCount, _cacheExpireSeconds, smSchemeDic, _useTrackEntity }) as IContextSpace;

              return cs;
        }
        static  IContextSpace CreateMySqlContext()
        {
            var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(SpaceFileName);
            var myType = myAssembly.GetType("Chloe.ObjectSpace.MysqlContextSpace");
            var cs = Activator.CreateInstance(myType, new object[] { ConnectionString, _cacheCount, _cacheExpireSeconds, smSchemeDic, _useTrackEntity }) as IContextSpace;

            return cs;
           
        }
        static  IContextSpace CreateOracleContext()
        {
            throw new NotImplementedException();
        }
        static  IContextSpace CreateSQLiteContext()
        {
            var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(SpaceFileName);
            var myType = myAssembly.GetType("Chloe.ObjectSpace.SqlliteContextSpace");
            var cs = Activator.CreateInstance(myType, new object[] { ConnectionString, _cacheCount, _cacheExpireSeconds, smSchemeDic, _useTrackEntity }) as IContextSpace;


              return cs;
        }

    }
}
